package com.alvin.demo.common.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 继承WebMvcConfigurationSupport的话配置比较麻烦 建议通过实现 WebMvcConfigurer 接口完成配置
 */

@Configuration
public class MvcConfig implements WebMvcConfigurer /* extends WebMvcConfigurationSupport */ {

	@Value("${file.uploadFolder}")
	private String uploadFolder;

	/**
	 * 设置静态资源映射
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		registry.addResourceHandler("/statics/**").addResourceLocations("classpath:/statics/");
		registry.addResourceHandler("/error/**").addResourceLocations("classpath:/errorPages/");
		registry.addResourceHandler("/favicon.ico").addResourceLocations("classpath:/statics/favicon.ico");
		// 外挂本地文件上传地址
		registry.addResourceHandler("/images/**").addResourceLocations("file:" + uploadFolder + "/");
	}

//  http请求时编码
//	@Override
//	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//		// 解决中文乱码
//		converters.add(responseBodyConverter());
//		// 解决 添加解决中文乱码后 上述配置之后，返回json数据直接报错 500
//		converters.add(messageConverter());
//	}
//
//	@Bean
//	public HttpMessageConverter<String> responseBodyConverter() {
//		return new StringHttpMessageConverter(StandardCharsets.UTF_8);
//	}
//
//	@Bean
//	public MappingJackson2HttpMessageConverter messageConverter() {
//		MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
//		converter.setObjectMapper(getObjectMapper());
//		return converter;
//	}
//
//	@Bean
//	public ObjectMapper getObjectMapper() {
//		return new ObjectMapper();
//	}
//
//	@Override
//	protected void addResourceHandlers(ResourceHandlerRegistry registry) {
//		registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
//		registry.addResourceHandler("/pages/**").addResourceLocations("classpath:/pages/");
//		registry.addResourceHandler("/images/**").addResourceLocations("file:" + uploadFolder + "/");
//		super.addResourceHandlers(registry);
//	}
}